package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.union.trade.OpenDayConfig;
import com.yanqu.road.entity.union.trade.UnionTradeEventInfo;
import com.yanqu.road.entity.union.trade.UnionTradeRouteInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UnionTradeConfigDaoImpl extends TempDao {

    public Map<Integer, UnionTradeRouteInfo> getUnionTradeRouteInfoMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, UnionTradeRouteInfo> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_p_union_trade_route";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionTradeRouteInfo data = new UnionTradeRouteInfo();
                data.setId(rs.getInt("id"));
                data.setName(rs.getString("name"));
                data.setUnlock(rs.getString("unlock"));
                data.setConsume(rs.getString("consume"));
                data.setCondition(rs.getLong("condition"));
                data.setGoTime(rs.getInt("goTime"));
                data.setReturnTime(rs.getInt("returnTime"));
                data.setRewards(rs.getString("rewards"));
                data.setEventNum(StringUtils.stringToIntegerList(rs.getString("eventNum"), ";"));
                data.setEventTime(StringUtils.stringToIntegerList(rs.getString("eventTime"), ";"));
                data.setEventList(StringUtils.stringToIntegerList(rs.getString("eventList"), ";"));
                data.setType(rs.getInt("type"));


                String condition = rs.getString("newCondition");
                List<OpenDayConfig> conditionConfigList =  new ArrayList<>();
                List<String> daysParamsList = StringUtils.stringToStringList(condition, "\\|");
                for (String params : daysParamsList) {
                    OpenDayConfig config = getOpenDayConfig(params);
                    conditionConfigList.add(config);
                }
                data.setConditionList(conditionConfigList);

                String rewards = rs.getString("newRewards");
                List<OpenDayConfig> rewardsConfigList =  new ArrayList<>();
                daysParamsList = StringUtils.stringToStringList(rewards, "\\|");
                for (String params : daysParamsList) {
                    OpenDayConfig config = getOpenDayConfig(params);
                    rewardsConfigList.add(config);
                }
                data.setRewardList(rewardsConfigList);
                dataMap.put(data.getId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    private OpenDayConfig getOpenDayConfig(String params) {
        List<String> paramList = StringUtils.stringToStringList(params, ";");
        OpenDayConfig config = new OpenDayConfig();
        config.setStart(Integer.parseInt(paramList.get(0)));
        config.setEnd(Integer.parseInt(paramList.get(1)));
        if(paramList.size() == 3){
            config.setParam(paramList.get(2));
        }else {
            List<String> itemList = new ArrayList<>();
            for (int i = 2; i < paramList.size(); i++) {
                itemList.add(paramList.get(i));
            }
            config.setParam(StringUtils.listToString(itemList, ";"));
        }
        return config;
    }

    public Map<Integer, UnionTradeEventInfo> getUnionTradeEventInfoMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, UnionTradeEventInfo> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_p_union_trade_event";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionTradeEventInfo data = new UnionTradeEventInfo();
                data.setId(rs.getInt("id"));
                data.setType(rs.getInt("type"));
                data.setRewards(rs.getString("rewards"));
                data.setRewardsNum(rs.getInt("rewardsNum"));
                dataMap.put(data.getId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }
}
